<?php
	require_once('config.inc.php');
	//include('/include/functions/initial_schedule.php');
	
	
	function initial_schedule ($semester_id, $building_id, $schedule_graduate, $schedule_honors, $schedule_levels, $strict_classroom_type)
	{
		/*get subjects for this bulding*/
		$subjectQuery = "SELECT cs.id
						 FROM course_subjects cs
						 JOIN building_subject_mapping bsm ON bsm.course_subject_id = cs.id
						 WHERE building_id = '$building_id'
						 AND priority = '1'";
		$subjectResult = mysql_query($subjectQuery);
		/*iterate through each subject*/
		while ($subjectRow = mysql_fetch_assoc($subjectResult))
		{
			$subject_id = $subjectRow['id'];
			
			/*get all unscheduled sections for this subject*/
			$sectionQuery = "SELECT *, MIN(graduate_level) AS grad_level
							 FROM course_sections csec
							 JOIN course_section_mapping csm ON csm.course_section_id = csec.id
							 JOIN courses c on c.id = csm.course_id
							 JOIN course_subjects csub ON csub.id = c.course_subject_id
							 WHERE semester_id = '$semester_id'
							 AND scheduling_rank = '1'
							 AND csub.id = '$subject_id'
							 AND csec.id NOT IN (SELECT course_section_id
							 					 FROM schedule)";
			/*if $schedule_graduate == 1, limit results to graduate level ONLY courses*/
			if ($schedule_graduate)
			{
				$sectionQuery .= " AND graduate_level = '1'";
			}
			/*if $schedule_graduate == 0, limit results to courses which contain undergad 
				(and possibly grad) level courses*/
			else
			{
				$sectionQuery .= " AND graduate_level = '0'";
			}
			/*if $schedule_honors == 1, limit results to honors program courses */
			if ($schedule_honors)
			{
				$sectionQuery .= " AND honors_program = '1'";
			}
			/*this ensures that each section only returns one row, regardless of the numebr of
				courses mapped to that section*/
			$sectionQuery .= " GROUP BY csec.id
							   ORDER BY course_number DESC";
			$sectionResult = mysql_query($sectionQuery);
			
			//echo $sectionQuery."<br /><br />".mysql_error();
			/*iterate through each section*/
			while ($sectionRow = mysql_fetch_assoc($sectionResult))
			{
				/*echo "<pre>";
				print_r($sectionRow);
				echo "</pre>";
				*/
				
				$classroom_type_id = $sectionRow['classroom_type_id'];
				$num_seats = $sectionRow['num_seats'];
				/*if instrcutor_id == 3, instructor is TBA*/
				$instructor_id = $sectionRow['instructor_id'];
				$availability = array();
				echo "<h4>$instructor_id</h4>";
					
				$availQuery = "SELECT *
							   FROM instructor_availability ia
							   JOIN days ON days.id = ia.day_id
							   WHERE instructor_id = '$instructor_id'
							   ORDER by day_id";
				$availResult = mysql_query($availQuery);
				echo mysql_error();
				$count = 0;
				$last_day_id = 0;
				while ($availRow = mysql_fetch_assoc($availResult))
				{
					if ($availRow['day_id'] != $last_day_id)
					{
						$count = 0;
					}
					$count++;
					
					$day_name = $availRow['day_name'];
					$start_time = $availRow['start_time'];
					$end_time = $availRow['end_time'];
					
					$availability[$day_name][$count]['start_time'] = $start_time;
					$availability[$day_name][$count]['end_time'] = $end_time;
					
					$last_day_id = $availRow['day_id'];
				}
				
				/*
				echo "<pre>";
				print_r($availability);
				echo "</pre>";
				*/
				
				
				$timeslotQuery = "SELECT *
								  FROM time_slots
								  WHERE building_id = '$building_id'";
				$timeslotResult = mysql_query($timeslotQuery);
				$valid_timeslots = array();
				while($timeslotRow = mysql_fetch_assoc($timeslotResult))
				{
					$ts_start_time = $timeslotRow['start_time'];
					$ts_end_time = $timeslotRow['end_time'];
					$Monday = $timeslotRow['Monday'];
					$Tuesday = $timeslotRow['Tuesday'];
					$Wednesday = $timeslotRow['Wednesday'];
					$Thursday = $timeslotRow['Thursday'];
					$Friday = $timeslotRow['Friday'];
					$Saturday = $timeslotRow['Saturday'];
					$Sunday = $timeslotRow['Sunday'];

					$ts_days = array('Monday' => $Monday, 'Tuesday' => $Tuesday, 'Wednesday' => $Wednesday, 'Thursday' => $Thursday, 'Friday' => $Friday, 'Saturday' => $Saturday, 'Sunday' => $Sunday);
					
					$valid_timeslot = TRUE;
					
					foreach ($ts_days as $day => $val)
					{
						$valid_for_this_day = TRUE;
						if ($val == '1')
						{
							$valid_for_this_day = FALSE;
							foreach ($availability[$day] as $avail_slot)
							{
								if ($ts_start_time >= $avail_slot['start_time'] && $ts_end_time <= $avail_slot['end_time'])
								{
									$valid_for_this_day = TRUE;
								}
							}
							//check if the instructor is already scheduled for a time that overlaps this timeslot
							$alreadyScheduledQuery = "SELECT *
													  FROM schedule 
													  JOIN course_sections cs ON cs.id = schedule.course_section_id
													  JOIN time_slots ts ON ts.id = schedule.time_slot_id
													  WHERE instructor_id = '$instructor_id'
													  AND semester_id = '$semester_id'";
							$alreadyScheduledResult = mysql_query($alreadyScheduledQuery);
							while ($asRow = mysql_fetch_assoc($alreadyScheduledResult))
							{
								if ($asRow[$day] == 1)
								{
									if (($asRow['start_time'] < $ts_start_time && $asRow['end_time'] > $ts_start_time) || ($asRow['start_time'] < $ts_end_time && $asRow['end_time'] > $ts_end_time) || ($asRow['start_time'] <= $ts_start_time && $asRow['end_time'] >= $ts_end_time) || ($asRow['start_time'] >= $ts_start_time && $asRow['end_time'] <= $ts_end_time))
									{
										
									}
								}
							}
							
							if (!$valid_for_this_day)
							{
								$valid_timeslot = FALSE;
							}
						}
					}
					
					/*If the timeslot fits the instructor's schedule and doesn't overlap with an already
					scheduled sections, push it on the array of valid timeslots for this section and instructor*/
					if ($valid_timeslot)
					{
						array_push($valid_timeslots, $timeslotRow['id']);	
						echo $timeslotRow['id'].', ';
					}
					
				}
				
				foreach ($valid_timeslots as $valid_timeslot_id)
				{
					$tsQuery = "SELECT *
							  FROM time_slots
							  WHERE id = '$valid_timeslot_id'";
					$tsResult = mysql_query($tsQuery);
					$tsRow = mysql_fetch_assoc($tsResult);
					$ts_start_time = $tsRow['start_time'];
					$ts_end_time = $tsRow['end_time'];
					//add more here
					
					
					/*get all classrooms in the building that have enough seats for the section*/
					$classroomQuery = "SELECT id
									   FROM classrooms 
									   WHERE building_id = '$building_id'
									   AND num_seats >= '$num_seats'";
					/*if $strict_classroom_type equals 1, only select classrooms of that classroom type*/
					if ($strict_classroom_type)
					{
						$classroomQuery .= " AND classroom_type_id = '$classroom_type_id'";
					}
					/*order smaller classrooms first so that you don't schedule a small section in a large room
					unless completely necessary*/
					$classroomQuery .= " ORDER BY num_seats ASC";
					$classroomResult = mysql_query($classroomQuery);
					while ($crRow = mysql_fetch_assoc($classroomResult))
					{
						$classroom_id = $crRow['id'];
						
						$scheduledCoursesQuery = "SELECT * FROM schedule sch
												  JOIN time_slots ts ON ts.id = sch.time_slot_id
												  WHERE classroom_id = '$classroom_id'";
						$scheduledCoursesResult = mysql_query($scheduledCoursesQuery);
						while ($scRow = mysql_fetch_assoc($scheduledCoursesResult))
						{
							$sc_start_time = $scRow['start_time'];
							$sc_end_time = $scRow['end_time'];
							$Monday = $scRow['Monday'];
							$Tuesday = $scRow['Tuesday'];
							$Wednesday = $scRow['Wednesday'];
							$Thursday = $scRow['Thursday'];
							$Friday = $scRow['Friday'];
							$Saturday = $scRow['Saturday'];
							$Sunday = $scRow['Sunday'];
						}
						
					}
				}
				
			}
		}
	}
	
	/*
	initial_schedule($building_id,1,0,1);
	initial_schedule($building_id,1,0,0);
	initial_schedule($building_id,0,1,0);
	initial_schedule($building_id,0,0,1);
	initial_schedule($building_id,0,0,0);
	second_pass($building_id);
	*/
	initial_schedule(1,1,0,0,0,1);
?>